{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "pointed-toilet",
   "metadata": {},
   "outputs": [],
   "source": [
    "from code.fttradeapi import tradeapi\n",
    "from futu import *\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "egyptian-webmaster",
   "metadata": {},
   "outputs": [],
   "source": [
    "tradeapi = tradeapi()\n",
    "#trade._init_context('US',TrdEnv.REAL)\n",
    "#tradeapi._init_context('HK', TrdEnv.REAL)\n",
    "\n",
    "#trade._init_context('HK',TrdEnv.REAL)\n",
    "\n",
    "#context.unlock_trade()\n",
    "\n",
    "# placeorder(self, price, qty, code, trd_side):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "administrative-mistake",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'tradeapi' object has no attribute 'context'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-5-770d96cb9241>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtradeapi\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcontext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_acc_list\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: 'tradeapi' object has no attribute 'context'"
     ]
    }
   ],
   "source": [
    "tradeapi.context.get_acc_list()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "reliable-resort",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'tradeapi' object has no attribute 'context'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-81dfb64e9e5c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mret\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdfasset\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtradeapi\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mquery_asset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0mdfasset\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/home/jupyter/code/fttradeapi.py\u001b[0m in \u001b[0;36mquery_asset\u001b[0;34m(self, acc_id, acc_index, refresh_cache)\u001b[0m\n\u001b[1;32m     51\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     52\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mquery_asset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0macc_id\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0macc_index\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrefresh_cache\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 53\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcontext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maccinfo_query\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrd_env\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0macc_id\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0macc_index\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrefresh_cache\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcurrency\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcurrency\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     54\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     55\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'tradeapi' object has no attribute 'context'"
     ]
    }
   ],
   "source": [
    "\n",
    "ret, dfasset = tradeapi.query_asset()\n",
    "dfasset\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "earned-supply",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "pointed-interaction",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'此交易市场不支持这种订单类型'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#elf, price, qty, code, trd_side,  order_type=OrderType.NORMAL, remark = 'auto',fill_outside_rth=True\n",
    "\n",
    "#ret, df = trade.placeorder(163,1,'US.FUTU',TrdSide.SELL, TrdEnv.REAL)\n",
    "\n",
    "ret, df = trade.placeorder(710,100,'HK.00700',TrdSide.BUY)\n",
    "#ret, df = trade.placeorder(0,100,'HK.00700',TrdSide.BUY, OrderType.MARKET) #'此交易市场不支持这种订单类型'\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "earlier-infrastructure",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code</th>\n",
       "      <th>stock_name</th>\n",
       "      <th>trd_side</th>\n",
       "      <th>order_type</th>\n",
       "      <th>order_status</th>\n",
       "      <th>order_id</th>\n",
       "      <th>qty</th>\n",
       "      <th>price</th>\n",
       "      <th>create_time</th>\n",
       "      <th>updated_time</th>\n",
       "      <th>dealt_qty</th>\n",
       "      <th>dealt_avg_price</th>\n",
       "      <th>last_err_msg</th>\n",
       "      <th>remark</th>\n",
       "      <th>time_in_force</th>\n",
       "      <th>fill_outside_rth</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HK.SMC210429C30000</td>\n",
       "      <td>中芯国际 210429 30.00 购</td>\n",
       "      <td>BUY</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FILLED_ALL</td>\n",
       "      <td>4933661508167327356</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.83</td>\n",
       "      <td>2021-04-07 09:34:28.335</td>\n",
       "      <td>2021-04-07 09:35:00.315</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.83</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>DAY</td>\n",
       "      <td>N/A</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 code           stock_name trd_side order_type order_status  \\\n",
       "0  HK.SMC210429C30000  中芯国际 210429 30.00 购      BUY     NORMAL   FILLED_ALL   \n",
       "\n",
       "              order_id  qty  price              create_time  \\\n",
       "0  4933661508167327356  1.0   0.83  2021-04-07 09:34:28.335   \n",
       "\n",
       "              updated_time  dealt_qty  dealt_avg_price last_err_msg remark  \\\n",
       "0  2021-04-07 09:35:00.315        1.0             0.83                       \n",
       "\n",
       "  time_in_force fill_outside_rth  \n",
       "0           DAY              N/A  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ret, dforders = tradeapi.query_orders()\n",
    "dforders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "automatic-twins",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0,\n",
       "   trd_env             order_id\n",
       " 0    REAL  7205367332206216581)"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " #ModifyOrderOp.CANCEL\n",
    " # ModifyOrderOp: NONE未知操作;NORMAL 修改订单的价格、数量; CANCEL 撤单; DISABLE 失效, ENABLE 生效; DELETE 删除\n",
    "\n",
    "#成交\n",
    "res = tradeapi.modify_order( ModifyOrderOp.CANCEL, '7205367332206216581', 0, 0)\n",
    "#ret, df = trade.modify_usorder( ModifyOrderOp.DELETE, '151552963758945457', 3, 165, trd_env = TrdEnv.REAL)\n",
    "\n",
    "#res = trade.modify_usorder( ModifyOrderOp.NORMAL, '2869890381890708737', 3, 166, trd_env = TrdEnv.REAL)\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "civic-sherman",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code</th>\n",
       "      <th>stock_name</th>\n",
       "      <th>qty</th>\n",
       "      <th>can_sell_qty</th>\n",
       "      <th>cost_price</th>\n",
       "      <th>cost_price_valid</th>\n",
       "      <th>market_val</th>\n",
       "      <th>nominal_price</th>\n",
       "      <th>pl_ratio</th>\n",
       "      <th>pl_ratio_valid</th>\n",
       "      <th>pl_val</th>\n",
       "      <th>pl_val_valid</th>\n",
       "      <th>today_buy_qty</th>\n",
       "      <th>today_buy_val</th>\n",
       "      <th>today_pl_val</th>\n",
       "      <th>today_sell_qty</th>\n",
       "      <th>today_sell_val</th>\n",
       "      <th>position_side</th>\n",
       "      <th>unrealized_pl</th>\n",
       "      <th>realized_pl</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HK.23932</td>\n",
       "      <td>华虹瑞信一四购A.C</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>0.3600</td>\n",
       "      <td>True</td>\n",
       "      <td>3100.0</td>\n",
       "      <td>0.310</td>\n",
       "      <td>-13.89</td>\n",
       "      <td>True</td>\n",
       "      <td>-500.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>450.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>LONG</td>\n",
       "      <td>N/A</td>\n",
       "      <td>N/A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HK.SMC210429C30000</td>\n",
       "      <td>中芯国际 210429 30.00 购</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.7650</td>\n",
       "      <td>True</td>\n",
       "      <td>3000.0</td>\n",
       "      <td>0.600</td>\n",
       "      <td>-66.01</td>\n",
       "      <td>True</td>\n",
       "      <td>-5825.0</td>\n",
       "      <td>True</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2075.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>LONG</td>\n",
       "      <td>N/A</td>\n",
       "      <td>N/A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HK.27649</td>\n",
       "      <td>中芯汇丰一乙购B.C</td>\n",
       "      <td>55000.0</td>\n",
       "      <td>55000.0</td>\n",
       "      <td>0.0940</td>\n",
       "      <td>True</td>\n",
       "      <td>9735.0</td>\n",
       "      <td>0.177</td>\n",
       "      <td>88.30</td>\n",
       "      <td>True</td>\n",
       "      <td>4565.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>605.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>LONG</td>\n",
       "      <td>N/A</td>\n",
       "      <td>N/A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HK.16031</td>\n",
       "      <td>中芯汇丰一乙购A.C</td>\n",
       "      <td>100000.0</td>\n",
       "      <td>100000.0</td>\n",
       "      <td>0.0690</td>\n",
       "      <td>True</td>\n",
       "      <td>7700.0</td>\n",
       "      <td>0.077</td>\n",
       "      <td>11.59</td>\n",
       "      <td>True</td>\n",
       "      <td>800.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>800.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>LONG</td>\n",
       "      <td>N/A</td>\n",
       "      <td>N/A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HK.22381</td>\n",
       "      <td>腾讯法巴二四购A.C</td>\n",
       "      <td>25000.0</td>\n",
       "      <td>25000.0</td>\n",
       "      <td>0.1946</td>\n",
       "      <td>True</td>\n",
       "      <td>3175.0</td>\n",
       "      <td>0.127</td>\n",
       "      <td>-34.74</td>\n",
       "      <td>True</td>\n",
       "      <td>-1690.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-375.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>LONG</td>\n",
       "      <td>N/A</td>\n",
       "      <td>N/A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>HK.HEX210429C510000</td>\n",
       "      <td>港交所 210429 510.00 购</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>27.5000</td>\n",
       "      <td>True</td>\n",
       "      <td>235.0</td>\n",
       "      <td>2.350</td>\n",
       "      <td>-91.45</td>\n",
       "      <td>True</td>\n",
       "      <td>-2515.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-80.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>LONG</td>\n",
       "      <td>N/A</td>\n",
       "      <td>N/A</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  code           stock_name       qty  can_sell_qty  \\\n",
       "0             HK.23932           华虹瑞信一四购A.C   10000.0       10000.0   \n",
       "1   HK.SMC210429C30000  中芯国际 210429 30.00 购       2.0           2.0   \n",
       "2             HK.27649           中芯汇丰一乙购B.C   55000.0       55000.0   \n",
       "3             HK.16031           中芯汇丰一乙购A.C  100000.0      100000.0   \n",
       "4             HK.22381           腾讯法巴二四购A.C   25000.0       25000.0   \n",
       "5  HK.HEX210429C510000  港交所 210429 510.00 购       1.0           1.0   \n",
       "\n",
       "   cost_price  cost_price_valid  market_val  nominal_price  pl_ratio  \\\n",
       "0      0.3600              True      3100.0          0.310    -13.89   \n",
       "1      1.7650              True      3000.0          0.600    -66.01   \n",
       "2      0.0940              True      9735.0          0.177     88.30   \n",
       "3      0.0690              True      7700.0          0.077     11.59   \n",
       "4      0.1946              True      3175.0          0.127    -34.74   \n",
       "5     27.5000              True       235.0          2.350    -91.45   \n",
       "\n",
       "   pl_ratio_valid  pl_val  pl_val_valid  today_buy_qty  today_buy_val  \\\n",
       "0            True  -500.0          True            0.0            0.0   \n",
       "1            True -5825.0          True            1.0         2075.0   \n",
       "2            True  4565.0          True            0.0            0.0   \n",
       "3            True   800.0          True            0.0            0.0   \n",
       "4            True -1690.0          True            0.0            0.0   \n",
       "5            True -2515.0          True            0.0            0.0   \n",
       "\n",
       "   today_pl_val  today_sell_qty  today_sell_val position_side unrealized_pl  \\\n",
       "0         450.0             0.0             0.0          LONG           N/A   \n",
       "1         200.0             0.0             0.0          LONG           N/A   \n",
       "2         605.0             0.0             0.0          LONG           N/A   \n",
       "3         800.0             0.0             0.0          LONG           N/A   \n",
       "4        -375.0             0.0             0.0          LONG           N/A   \n",
       "5         -80.0             0.0             0.0          LONG           N/A   \n",
       "\n",
       "  realized_pl  \n",
       "0         N/A  \n",
       "1         N/A  \n",
       "2         N/A  \n",
       "3         N/A  \n",
       "4         N/A  \n",
       "5         N/A  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ret, dfpos = tradeapi.query_position()\n",
    "dfpos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "perceived-thomas",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code</th>\n",
       "      <th>stock_name</th>\n",
       "      <th>qty</th>\n",
       "      <th>can_sell_qty</th>\n",
       "      <th>cost_price</th>\n",
       "      <th>cost_price_valid</th>\n",
       "      <th>market_val</th>\n",
       "      <th>nominal_price</th>\n",
       "      <th>pl_ratio</th>\n",
       "      <th>pl_ratio_valid</th>\n",
       "      <th>pl_val</th>\n",
       "      <th>pl_val_valid</th>\n",
       "      <th>today_buy_qty</th>\n",
       "      <th>today_buy_val</th>\n",
       "      <th>today_pl_val</th>\n",
       "      <th>today_sell_qty</th>\n",
       "      <th>today_sell_val</th>\n",
       "      <th>position_side</th>\n",
       "      <th>unrealized_pl</th>\n",
       "      <th>realized_pl</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HK.23932</td>\n",
       "      <td>华虹瑞信一四购A.C</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>0.3600</td>\n",
       "      <td>True</td>\n",
       "      <td>3100.0</td>\n",
       "      <td>0.310</td>\n",
       "      <td>-13.89</td>\n",
       "      <td>True</td>\n",
       "      <td>-500.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>450.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>LONG</td>\n",
       "      <td>N/A</td>\n",
       "      <td>N/A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HK.SMC210429C30000</td>\n",
       "      <td>中芯国际 210429 30.00 购</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.7650</td>\n",
       "      <td>True</td>\n",
       "      <td>3000.0</td>\n",
       "      <td>0.600</td>\n",
       "      <td>-66.01</td>\n",
       "      <td>True</td>\n",
       "      <td>-5825.0</td>\n",
       "      <td>True</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2075.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>LONG</td>\n",
       "      <td>N/A</td>\n",
       "      <td>N/A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HK.27649</td>\n",
       "      <td>中芯汇丰一乙购B.C</td>\n",
       "      <td>55000.0</td>\n",
       "      <td>55000.0</td>\n",
       "      <td>0.0940</td>\n",
       "      <td>True</td>\n",
       "      <td>9735.0</td>\n",
       "      <td>0.177</td>\n",
       "      <td>88.30</td>\n",
       "      <td>True</td>\n",
       "      <td>4565.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>605.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>LONG</td>\n",
       "      <td>N/A</td>\n",
       "      <td>N/A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HK.16031</td>\n",
       "      <td>中芯汇丰一乙购A.C</td>\n",
       "      <td>100000.0</td>\n",
       "      <td>100000.0</td>\n",
       "      <td>0.0690</td>\n",
       "      <td>True</td>\n",
       "      <td>7700.0</td>\n",
       "      <td>0.077</td>\n",
       "      <td>11.59</td>\n",
       "      <td>True</td>\n",
       "      <td>800.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>800.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>LONG</td>\n",
       "      <td>N/A</td>\n",
       "      <td>N/A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HK.22381</td>\n",
       "      <td>腾讯法巴二四购A.C</td>\n",
       "      <td>25000.0</td>\n",
       "      <td>25000.0</td>\n",
       "      <td>0.1946</td>\n",
       "      <td>True</td>\n",
       "      <td>3175.0</td>\n",
       "      <td>0.127</td>\n",
       "      <td>-34.74</td>\n",
       "      <td>True</td>\n",
       "      <td>-1690.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-375.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>LONG</td>\n",
       "      <td>N/A</td>\n",
       "      <td>N/A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>HK.HEX210429C510000</td>\n",
       "      <td>港交所 210429 510.00 购</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>27.5000</td>\n",
       "      <td>True</td>\n",
       "      <td>235.0</td>\n",
       "      <td>2.350</td>\n",
       "      <td>-91.45</td>\n",
       "      <td>True</td>\n",
       "      <td>-2515.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-80.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>LONG</td>\n",
       "      <td>N/A</td>\n",
       "      <td>N/A</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  code           stock_name       qty  can_sell_qty  \\\n",
       "0             HK.23932           华虹瑞信一四购A.C   10000.0       10000.0   \n",
       "1   HK.SMC210429C30000  中芯国际 210429 30.00 购       2.0           2.0   \n",
       "2             HK.27649           中芯汇丰一乙购B.C   55000.0       55000.0   \n",
       "3             HK.16031           中芯汇丰一乙购A.C  100000.0      100000.0   \n",
       "4             HK.22381           腾讯法巴二四购A.C   25000.0       25000.0   \n",
       "5  HK.HEX210429C510000  港交所 210429 510.00 购       1.0           1.0   \n",
       "\n",
       "   cost_price  cost_price_valid  market_val  nominal_price  pl_ratio  \\\n",
       "0      0.3600              True      3100.0          0.310    -13.89   \n",
       "1      1.7650              True      3000.0          0.600    -66.01   \n",
       "2      0.0940              True      9735.0          0.177     88.30   \n",
       "3      0.0690              True      7700.0          0.077     11.59   \n",
       "4      0.1946              True      3175.0          0.127    -34.74   \n",
       "5     27.5000              True       235.0          2.350    -91.45   \n",
       "\n",
       "   pl_ratio_valid  pl_val  pl_val_valid  today_buy_qty  today_buy_val  \\\n",
       "0            True  -500.0          True            0.0            0.0   \n",
       "1            True -5825.0          True            1.0         2075.0   \n",
       "2            True  4565.0          True            0.0            0.0   \n",
       "3            True   800.0          True            0.0            0.0   \n",
       "4            True -1690.0          True            0.0            0.0   \n",
       "5            True -2515.0          True            0.0            0.0   \n",
       "\n",
       "   today_pl_val  today_sell_qty  today_sell_val position_side unrealized_pl  \\\n",
       "0         450.0             0.0             0.0          LONG           N/A   \n",
       "1         200.0             0.0             0.0          LONG           N/A   \n",
       "2         605.0             0.0             0.0          LONG           N/A   \n",
       "3         800.0             0.0             0.0          LONG           N/A   \n",
       "4        -375.0             0.0             0.0          LONG           N/A   \n",
       "5         -80.0             0.0             0.0          LONG           N/A   \n",
       "\n",
       "  realized_pl  \n",
       "0         N/A  \n",
       "1         N/A  \n",
       "2         N/A  \n",
       "3         N/A  \n",
       "4         N/A  \n",
       "5         N/A  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#dfpos[(dfpos.qty > 0 ) & (dfpos.stock_name.str.contains('腾讯'))]\n",
    "dfpos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "swiss-syracuse",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'REAL'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "defensive-smith",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "solved-preserve",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0,                   code          stock_name trd_side order_type order_status  \\\n",
      "0  HK.TCH210330C710000  腾讯 210330 710.00 购     SELL     NORMAL   SUBMITTING   \n",
      "\n",
      "            order_id  qty  price          create_time         updated_time  \\\n",
      "0  87490750911280625  1.0   32.0  2021-03-03 14:29:32  2021-03-03 14:29:32   \n",
      "\n",
      "   dealt_qty  dealt_avg_price last_err_msg remark time_in_force  \\\n",
      "0        0.0              0.0                auto           DAY   \n",
      "\n",
      "  fill_outside_rth  \n",
      "0              N/A  )\n",
      "TradeDealTest content=  trd_env                 code          stock_name              deal_id  \\\n",
      "0    REAL  HK.TCH210330C710000  腾讯 210330 710.00 购  2359256070363779605   \n",
      "\n",
      "            order_id  qty  price trd_side              create_time  \\\n",
      "0  87490750911280625  1.0   32.0     SELL  2021-03-03 14:29:32.656   \n",
      "\n",
      "  counter_broker_id counter_broker_name trd_market status  \n",
      "0               N/A                 N/A         HK     OK  \n",
      "TradeDealTest content=  trd_env      code  stock_name              deal_id             order_id  \\\n",
      "0    REAL  HK.52667  港交高盛一八牛K.C  5749621375642766284  8428814502847337943   \n",
      "\n",
      "       qty  price trd_side              create_time  counter_broker_id  \\\n",
      "0  10000.0  0.066     SELL  2021-03-03 14:42:41.298               1697   \n",
      "\n",
      "  counter_broker_name trd_market status  \n",
      "0                  宇博         HK     OK  \n"
     ]
    }
   ],
   "source": [
    "from futu import *\n",
    "from time import sleep\n",
    "class TradeDealTest(TradeDealHandlerBase):\n",
    "  \"\"\" order update push\"\"\"\n",
    "  def on_recv_rsp(self, rsp_pb):\n",
    "        ret, content = super(TradeDealTest, self).on_recv_rsp(rsp_pb)\n",
    "\n",
    "        if ret == RET_OK:\n",
    "              print(\"TradeDealTest content={}\".format(content))\n",
    "\n",
    "        return ret, content\n",
    "\n",
    "tradeapi.context.set_handler(TradeDealTest())\n",
    "print(tradeapi.placeorder(price=32, qty=1, code=\"HK.TCH210330C710000\", trd_side=TrdSide.SELL))\n",
    "\n",
    "sleep(180)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "special-shock",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-03-11 17:53:46,928 [network_manager.py] work:366: Close: conn_id=2\n"
     ]
    }
   ],
   "source": [
    "res = tradeapi.closecontext()\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "monetary-teaching",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-03-02 14:20:06,669 [open_context_base.py] _socket_reconnect_and_wait_ready:255: Start connecting: host=127.0.0.1; port=11111;\n",
      "2021-03-02 14:20:06,674 [open_context_base.py] on_connected:344: Connected : conn_id=3; \n",
      "2021-03-02 14:20:06,678 [open_context_base.py] _handle_init_connect:445: InitConnect ok: conn_id=3; info={'server_version': 500, 'login_user_id': 716900, 'conn_id': 6772400090471689235, 'conn_key': '903A605FDA92D406', 'conn_iv': 'ABAD325618A02694', 'keep_alive_interval': 10, 'is_encrypt': False};\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "* TradeOrderTest content=  trd_env      code  stock_name  dealt_avg_price  dealt_qty      qty  \\\n",
      "0    REAL  HK.52667  港交高盛一八牛K.C              0.0        0.0  10000.0   \n",
      "\n",
      "              order_id order_type  price order_status          create_time  \\\n",
      "0  5553950296976792098     NORMAL  0.045   SUBMITTING  2021-03-02 14:20:06   \n",
      "\n",
      "          updated_time trd_side last_err_msg trd_market remark time_in_force  \\\n",
      "0  2021-03-02 14:20:06      BUY                      HK                  DAY   \n",
      "\n",
      "  fill_outside_rth  \n",
      "0              N/A  \n",
      "\n",
      "(0,        code  stock_name trd_side order_type order_status             order_id  \\\n",
      "0  HK.52667  港交高盛一八牛K.C      BUY     NORMAL   SUBMITTING  5553950296976792098   \n",
      "\n",
      "       qty  price          create_time         updated_time  dealt_qty  \\\n",
      "0  10000.0  0.045  2021-03-02 14:20:06  2021-03-02 14:20:06        0.0   \n",
      "\n",
      "   dealt_avg_price last_err_msg remark time_in_force fill_outside_rth  \n",
      "0              0.0                               DAY              N/A  )\n",
      "* TradeOrderTest content=  trd_env      code  stock_name  dealt_avg_price  dealt_qty      qty  \\\n",
      "0    REAL  HK.52667  港交高盛一八牛K.C            0.045    10000.0  10000.0   \n",
      "\n",
      "              order_id order_type  price order_status  \\\n",
      "0  5553950296976792098     NORMAL  0.045   FILLED_ALL   \n",
      "\n",
      "               create_time             updated_time trd_side last_err_msg  \\\n",
      "0  2021-03-02 14:20:06.719  2021-03-02 14:20:06.859      BUY                \n",
      "\n",
      "  trd_market remark time_in_force fill_outside_rth  \n",
      "0         HK                  DAY              N/A  \n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-03-02 14:20:21,913 [network_manager.py] work:366: Close: conn_id=3\n"
     ]
    }
   ],
   "source": [
    "from futu import *\n",
    "class TradeOrderTest(TradeOrderHandlerBase):\n",
    "    \"\"\" order update push\"\"\"\n",
    "    def on_recv_rsp(self, rsp_pb):\n",
    "        ret, content = super(TradeOrderTest, self).on_recv_rsp(rsp_pb)\n",
    "\n",
    "        if ret == RET_OK:\n",
    "            print(\"* TradeOrderTest content={}\\n\".format(content))\n",
    "\n",
    "        return ret, content\n",
    "\n",
    "trd_ctx = OpenHKTradeContext(host='127.0.0.1', port=11111)\n",
    "trd_ctx.set_handler(TradeOrderTest())\n",
    "#print(trd_ctx.unlock_trade(pwd_unlock))\n",
    "#print(trade.place_order(price=0.45, qty=10000, code=\"HK.52667\", trd_side=TrdSide.BUY))\n",
    "print(trd_ctx.place_order(price=0.045, qty=10000, code=\"HK.52667\", trd_side=TrdSide.BUY, trd_env=TrdEnv.REAL))\n",
    "\n",
    "sleep(15)\n",
    "trd_ctx.close()"
   ]
  },
  {
   "cell_type": "raw",
   "id": "peaceful-electricity",
   "metadata": {},
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "surprised-mercy",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-03-01 18:16:53,717 [open_context_base.py] _socket_reconnect_and_wait_ready:255: Start connecting: host=127.0.0.1; port=11111;\n",
      "2021-03-01 18:16:53,720 [open_context_base.py] on_connected:344: Connected : conn_id=9; \n",
      "2021-03-01 18:16:53,725 [open_context_base.py] _handle_init_connect:445: InitConnect ok: conn_id=9; info={'server_version': 500, 'login_user_id': 716900, 'conn_id': 6772097291280680708, 'conn_key': '1A8612B1D8920462', 'conn_iv': 'B4F83E18096806A2', 'keep_alive_interval': 10, 'is_encrypt': False};\n"
     ]
    }
   ],
   "source": [
    "hktrade = tradeapi()\n",
    "hktrade._init_context('HK')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "mobile-profile",
   "metadata": {},
   "outputs": [],
   "source": [
    "ret, df = hktrade.placeorder(700,100,'HK.00700',TrdSide.BUY, TrdEnv.SIMULATE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "distant-orange",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'SIMULATE'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "trade.trd_env"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
